package com.mall.server.controller.pms;

import cn.hutool.core.convert.Convert;
import com.mall.model.domain.PmsProductAttributeCategory;
import com.mall.model.vo.CommonPage;
import com.mall.model.vo.CommonResult;
import com.mall.model.vo.ProductAttributeCategoryWithAttrVo;
import com.mall.server.service.pms.PmsProductAttributeCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/productAttribute/category")
public class PmsProductAttributeCategoryController {

    @Autowired
    private PmsProductAttributeCategoryService categoryService;

    @PostMapping("/create")
    public CommonResult addProductAttributeCategory(String name) {

        categoryService.addProductAttributeCategory(name);
        return CommonResult.success(null);
    }

    @GetMapping("/delete/{id}")
    public CommonResult deleteCategory(@PathVariable("id") Integer id) {

        categoryService.deleteCategory(Convert.toLong(id));
        return CommonResult.success(null);
    }

    @GetMapping("/list")
    public CommonResult selectList(@RequestParam(defaultValue = "1") Integer pageNum
            , @RequestParam(defaultValue = "10") Integer pageSize) {

        CommonPage<PmsProductAttributeCategory> pageInfo = categoryService.selectList(pageNum, pageSize);
        return CommonResult.success(pageInfo);
    }

    //获取所有商品属性分类及其下属性
    @GetMapping("/list/withAttr")
    public CommonResult selectProductCategoryWithAttr() {

        List<ProductAttributeCategoryWithAttrVo> vos = categoryService.selectProductCategoryWithAttr();
        return CommonResult.success(vos);
    }


    @PostMapping("/update/{id}")
    public CommonResult updateCategory(@PathVariable("id") Integer id, String name) {

        categoryService.updateCategory(id, name);
        return CommonResult.success(null);
    }

    @GetMapping("/{id}")
    public CommonResult findCategoryById(@PathVariable("id") Integer id) {

        PmsProductAttributeCategory category = categoryService.findCategoryById(Convert.toLong(id));
        return CommonResult.success(category);
    }

}
